package com.batch.demo.domain.jobFlowDemo2;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.builder.FlowBuilder;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class JobFlowDemo2 {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job jobFlowDemo2Job(){
        return jobBuilderFactory.get("jobFlowDemo2Job")
                .start(jobFlowDemoFlow1())
                .next(jobFlowDemo2Step3()).end()
                .build();

    }

    @Bean
    public Flow jobFlowDemoFlow1(){
        return new FlowBuilder<Flow>("jobFlowDemoFlow1")
                .start(jobFlowDemo2Step1())
                .next(jobFlowDemo2Step2())
                .build();
    }

    @Bean
    public Step jobFlowDemo2Step1(){
        return stepBuilderFactory.get("jobFlowDemo2Step1")
               .tasklet(((stepContribution, chunkContext) -> {
                   System.out.println("jobFlowDemo2Step1");
                   return RepeatStatus.FINISHED;
               })).build();
    }

    @Bean
    public Step jobFlowDemo2Step2(){
        return stepBuilderFactory.get("jobFlowDemo2Step2")
                .tasklet(((stepContribution, chunkContext) -> {
                    System.out.println("jobFlowDemo2Step2");
                    return RepeatStatus.FINISHED;
                })).build();
    }

    @Bean
    public Step jobFlowDemo2Step3(){
        return stepBuilderFactory.get("jobFlowDemo2Step3")
                .tasklet(((stepContribution, chunkContext) -> {
                    System.out.println("jobFlowDemo2Step3");
                    return RepeatStatus.FINISHED;
                })).build();
    }

}
